package com.lza.phone.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lza.handle.FromTokenGetUser;
import com.lza.phone.entity.Capacity;
import com.lza.phone.entity.Phone;
import com.lza.phone.entity.User;
import com.lza.phone.service.CapacityService;
import com.lza.phone.service.PhoneService;
import com.lza.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李钊澳
 * @since 2021-09-05
 */
@RestController
@RequestMapping("/capacity")

public class CapacityController {
    @Autowired
    FromTokenGetUser fromTokenGetUser;
    @Autowired
    CapacityService capacityService;
    @Autowired
    PhoneService phoneService;
    @PostMapping("showPkPhone/{userId}")

    public Result showPkPhone(@PathVariable("userId") Integer userId){
        List<Capacity> capacities = capacityService.showPkPhone(userId);
        if(capacities.isEmpty()){
            return Result.error().getMessage("您没有加入要对比的手机");
        }else{
            return Result.ok().getData("要对比的手机为", capacities);
        }
    }
   /* //选择该手机是否加入容器
    @PostMapping("/change/{phoneId}")
    public Result change(@PathVariable("phoneId") int phoneId) {
        QueryWrapper<Capacity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phoneId", phoneId);
        //先查找是否有该手机
        Phone phone = phoneService.getById(phoneId);
        if(phone==null) {
            return Result.error().getMessage("没有该手机");
        }else{
            capacityService.update(queryWrapper)
    }
    }
*/
   //添加某手机到容器库
   @PostMapping("/addOnePhone/{phoneId}")
   public Result addOnePhone(@PathVariable("phoneId") int phoneId){
       User user = fromTokenGetUser.getUser();
       Capacity capacity=new Capacity();
       capacity.setPhoneId(phoneId);
       capacity.setUserId(user.getId());
       capacityService.save(capacity);
       return Result.ok();
   }
   //删除手机在容量库
    @PostMapping("/deleteOnePhone/{phoneId}")
    public Result deleteOnePhone(@PathVariable("phoneId") int phoneId) {
        User user = fromTokenGetUser.getUser();
        QueryWrapper<Capacity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phoneId", phoneId);
        boolean remove = capacityService.remove(queryWrapper);
        if (remove == false) {
            return Result.error().getMessage("该手机没有加入容器");
        } else {
            return Result.ok();
        }
    }

}

